/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package socof_webcrawler;

import DataBase.DBAccess;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URLDecoder;

public class SOCOF_WebCrawler {

    public static String _DOMAIN;
    public static String _PATH;
    public static Integer _LEVEL;
    public static Boolean _INIT;
    public static Boolean _STOPPER;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        StringBuilder stringBuilder = new StringBuilder();
        int count = args.length;


        if (count < 3 && args[0].isEmpty()
                || args[1].isEmpty()
                || args[2].isEmpty()) {
            String ls = System.getProperty("line.separator");
            stringBuilder.append("webCrawler <site> <folder> <nivel de download> <Downloaders> <Parsers>, <Bandwidth>");
            stringBuilder.append(ls);
            stringBuilder.append(ls);
        
            stringBuilder.append("<site> - webSite domain");
            stringBuilder.append(ls);
            stringBuilder.append("<folder> - folder where to locally store the website files");
            stringBuilder.append(ls);
            stringBuilder.append("<nivel de download> - level to stop downloading (domain = 0 || domain/images/* = 1)");
            stringBuilder.append(ls);


            stringBuilder.append("Optional values");
            stringBuilder.append(ls);
            stringBuilder.append("<Downloaders> - number of downloaders threads");
            stringBuilder.append(ls);
            stringBuilder.append("<Parsers> - number of parsers threads");
            stringBuilder.append(ls);
            stringBuilder.append("<Bandwidth> - Bandwith available to download (kbytes))");
            stringBuilder.append(ls);
        } else {
            if (!(args[0].toLowerCase().contains("http://") || args[0].toLowerCase().contains("https://"))) {

                args[0] = "http://" + args[0];
            }
//            boolean result = false;
//            String cursor[] = {".htm", "html", ".asp", ".php", ".jpg", ".gif", ".png", "zip"};
//            for (int i = 0; i < cursor.length; i++) {
//                if (args[0].contains(cursor[i])) {
//                    result = true;
//                }
//            }
//            String ficheiro;
//            if (result == false) {
//                if (!args[0].endsWith("/")) {
//                    args[0] += "/";
//                }
//                ficheiro = args[0] + "index.html";
//            } else {
//                ficheiro = args[0];
//            }

            DBAccess db = new DBAccess();
            db.init();
            db.deleteTable("FICHEIROS");
            db.criaTabelaOmissao();

            int bandwidth = 100000;
            int dwThreads = 2;
            int prThreads = 3;


            URLDecoder t = new URLDecoder();
            String test = t.decode(args[0]);
            setDOMAIN(args[0]);
            setPATH(args[1]);
            setLEVEL(Integer.parseInt(args[2]));
            setINIT(true);
            setSTOPPER(false);

            if (count > 3 && count >= 6) {
                if (!args[3].isEmpty()) {
                    if (Integer.parseInt(args[3]) > 0) {
                        dwThreads = Integer.parseInt(args[3]);
                    }
                }


                if (!args[4].isEmpty()) {
                    if (Integer.parseInt(args[4]) > 0) {
                        prThreads = Integer.parseInt(args[4]);
                    }
                }

                if (!args[5].isEmpty()) {
                    if (Integer.parseInt(args[5]) > 0) {
                        bandwidth = Integer.parseInt(args[5]);
                    }
                }
            }
            Laucher lb = Laucher.getInstance();
            lb.setMaxBandwithAllocation(bandwidth);
            lb.setMaxCpuUsage(0.1);
            lb.setDownloadersLimit(dwThreads);
            lb.setParsersLimit(prThreads);
            lb.start();

            System.out.println("Enter something to finish: ");
            InputStreamReader converter = new InputStreamReader(System.in);
            BufferedReader in = new BufferedReader(converter);
            in.readLine();
            lb.interrupt();
            db.shutdown();
        }
    }

    public static Boolean getSTOPPER() {
        return _STOPPER;
    }

    public static void setSTOPPER(Boolean _STOPPER) {
        SOCOF_WebCrawler._STOPPER = _STOPPER;
    }

    public static Boolean getINIT() {
        return _INIT;
    }

    public static void setINIT(Boolean _INIT) {
        SOCOF_WebCrawler._INIT = _INIT;
    }

    public static Integer getLEVEL() {
        return _LEVEL;
    }

    public static void setLEVEL(Integer _LEVEL) {
        SOCOF_WebCrawler._LEVEL = _LEVEL;
    }

    public static String getDOMAIN() {
        return _DOMAIN;
    }

    public static void setDOMAIN(String domain) {
        _DOMAIN = domain;
    }

    public static String getPATH() {
        return _PATH;
    }

    public static void setPATH(String _PATH) {
        SOCOF_WebCrawler._PATH = _PATH;
    }
}
